﻿namespace ACCOUNTING.Encryption
{
    using Microsoft.VisualBasic.CompilerServices;
    using System;
    using System.Configuration;
    using System.Runtime.InteropServices;
    using System.Text;
    using System.Text.RegularExpressions;

    internal class Utils
    {
        internal static byte[] FromBase64(string base64Encoded)
        {
            byte[] buffer;
            if ((base64Encoded == null) || (base64Encoded.Length == 0))
            {
                return null;
            }
            try
            {
                buffer = Convert.FromBase64String(base64Encoded);
            }
            catch (FormatException exception1)
            {
                ProjectData.SetProjectError(exception1);
                FormatException innerException = exception1;
                throw new FormatException("The provided string does not appear to be Base64 encoded:" + Environment.NewLine + base64Encoded + Environment.NewLine, innerException);
                ProjectData.ClearProjectError();
            }
            return buffer;
        }

        internal static byte[] FromHex(string hexEncoded)
        {
            byte[] buffer;
            if ((hexEncoded == null) || (hexEncoded.Length == 0))
            {
                return null;
            }
            try
            {
                int num = Convert.ToInt32((double) (((double) hexEncoded.Length) / 2.0));
                byte[] buffer2 = new byte[(num - 1) + 1];
                int num3 = num - 1;
                for (int i = 0; i <= num3; i++)
                {
                    buffer2[i] = Convert.ToByte(hexEncoded.Substring(i * 2, 2), 0x10);
                }
                buffer = buffer2;
            }
            catch (Exception exception1)
            {
                ProjectData.SetProjectError(exception1);
                Exception innerException = exception1;
                throw new FormatException("The provided string does not appear to be Hex encoded:" + Environment.NewLine + hexEncoded + Environment.NewLine, innerException);
                ProjectData.ClearProjectError();
            }
            return buffer;
        }

        internal static string GetConfigString(string key, bool isRequired = true)
        {
            string str2 = ConfigurationSettings.AppSettings.Get(key);
            if (str2 == null)
            {
                if (isRequired)
                {
                    throw new ConfigurationException("key <" + key + "> is missing from .config file");
                }
                return "";
            }
            return str2;
        }

        internal static string GetXmlElement(string xml, string element)
        {
            Match match = Regex.Match(xml, "<" + element + ">(?<Element>[^>]*)</" + element + ">", RegexOptions.IgnoreCase);
            if (match == null)
            {
                throw new Exception("Could not find <" + element + "></" + element + "> in provided Public Key XML.");
            }
            return match.Groups["Element"].ToString();
        }

        internal static string ToBase64(byte[] b)
        {
            if ((b == null) || (b.Length == 0))
            {
                return "";
            }
            return Convert.ToBase64String(b);
        }

        internal static string ToHex(byte[] ba)
        {
            if ((ba == null) || (ba.Length == 0))
            {
                return "";
            }
            StringBuilder builder = new StringBuilder();
            foreach (byte num in ba)
            {
                builder.Append(string.Format("{0:X2}", num));
            }
            return builder.ToString();
        }

        internal static string WriteConfigKey(string key, string value)
        {
            return string.Format("<add key=\"{0}\" value=\"{1}\" />" + Environment.NewLine, key, value);
        }

        internal static string WriteXmlElement(string element, string value)
        {
            return string.Format("<{0}>{1}</{0}>" + Environment.NewLine, element, value);
        }

        internal static string WriteXmlNode(string element, bool isClosing = false)
        {
            string str;
            if (isClosing)
            {
                str = "</{0}>" + Environment.NewLine;
            }
            else
            {
                str = "<{0}>" + Environment.NewLine;
            }
            return string.Format(str, element);
        }
    }
}

